package com.maibow.controller.weixin;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.controller.base.BaseController;
import com.maibow.entity.Page;
import com.maibow.interceptor.Token;
import com.maibow.service.organ.OrganService;
import com.maibow.service.system.CommonService;
import com.maibow.service.weixin.WeixinActivityValidService;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.party
 * 文件名：CommercialActivityPublishController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月22日-下午8:27:42
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/weixinActValid")
public class WeixinActivityValidController extends BaseController {

    @Resource(name = "weixinActivityValidService")
    private WeixinActivityValidService weixinActivityValidService;

    @Resource(name = "commonService")
    private CommonService commonService;

    @Resource(name = "organService")
    private OrganService organService;

    /**
     * 
     * queryWeixinActivityValidlistPage(
     * 这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param session
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/list")
    public ModelAndView queryWeixinActivityValidlistPage(HttpSession session, Page page) throws Exception {
        logBefore(logger, "微信公众号活动列表");
        ModelAndView modelView = new ModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            if (null != pageData.get("activity_name") && !"".equals(pageData.get("activity_name"))) {
                pageData.put("activity_name", pageData.getString("activity_name"));
            }
            page.setPageData(pageData);
            List<PageData> varList = weixinActivityValidService.queryWeixinActivityValidlistPage(page);
            modelView.setViewName("system/weixin/weixin_act_valid_list");
            modelView.addObject("varList", varList);
            modelView.addObject("pageData", pageData);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return modelView;
    }

    @Token(save = true)
    @RequestMapping(value = "/goAddWeixinActivityValid")
    public ModelAndView goAddWeixinActivityValid() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        modelView.setViewName("system/weixin/weixin_act_valid_add");
        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * saveUser(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(remove = true)
    @RequestMapping(value = "/saveWeixinActivityValid")
    public ModelAndView saveWeixinActivityValid(HttpServletRequest request, @RequestParam(value = "channel", required = false) String channel,
                        @RequestParam(value = "activity_name", required = false) String activity_name) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        pageData.put("activity_name", activity_name);
        pageData.put("channel", Integer.valueOf(channel));
        pageData.put("valid_state", 0);

        // 创建公众号活动
        weixinActivityValidService.saveWeixinActivityValid(pageData);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * goEdit(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/goEdit")
    public ModelAndView goEdit() {
        logBefore(logger, "去修改活动状态页面");
        ModelAndView mv = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        try {
            pageData.put("act_valid_id", Long.valueOf(pageData.get("act_valid_id").toString()));
            pageData = weixinActivityValidService.findWeixinActivityValid(pageData); // 根据ID读取
            mv.setViewName("system/weixin/weixin_act_valid_edit");
            mv.addObject("msg", "edit");
            mv.addObject("pageData", pageData);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 
     * editWeixinActivityValid(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/editWeixinActivityValid")
    public ModelAndView editWeixinActivityValid(PrintWriter out) throws Exception {
        logBefore(logger, "修改活动状态");
        ModelAndView mv = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("valid_state", Integer.valueOf(pageData.get("valid_state").toString()));
        pageData.put("act_valid_id", Integer.valueOf(pageData.get("act_valid_id").toString()));
        weixinActivityValidService.updateWeixinActivityValidState(pageData);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }
}
